#include "mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QAction>
#include <QToolBar>
#include <QApplication>
#include <QDesktopWidget>
#include <QColor>
#include <QMenu>
#include <QMenuBar>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    this->setObjectName("mainWindow");
    this->setWindowTitle("山东理工大学新生导游系统--已授权");
    //this->centralWidget()
    resize(1100,800);
    move ((QApplication::desktop()->width() - width())/2,(QApplication::desktop()->height() - height())/2);

    QAction *PlaceAction = new QAction("景点管理",this);
    QAction *PlanAction = new QAction("线路规划",this);
    QAction *BicycleAction = new QAction("单车管理",this);
    QAction *AboutAction = new QAction("关于",this);
    QAction *BusAction = new QAction("校车管理",this);
    QAction *DatainitAction = new QAction("数据初始化");
    QAction *helpAction = new QAction("使用说明");

    QMenuBar *menuBar = this->menuBar();
    QMenu *settingMenu = menuBar->addMenu("系统设置");
    settingMenu->addAction(PlaceAction);
    settingMenu->addAction(BicycleAction);
    settingMenu->addAction(BusAction);
    settingMenu->addSeparator();
    settingMenu->addAction(DatainitAction);
    QMenu *guideMenu = menuBar->addMenu("校园导游");
    guideMenu->addAction(PlanAction);
    QMenu *helpMenu = menuBar->addMenu("帮助");
    helpMenu->addAction(AboutAction);
    helpMenu->addAction(helpAction);

    //QToolBar *PlaceBar = this->addToolBar("校园景点");
    //QToolBar *PlanBar = this->addToolBar("路线规划");
    //QToolBar *BicycleBar = this->addToolBar("共享单车管理");
//    QToolBar *AboutBar = this->addToolBar("关于");
    //PlaceBar->addAction(PlaceAction);
    //PlanBar->addAction(PlanAction);
    //BicycleBar->addAction(BicycleAction);
//    AboutBar->addAction(AboutAction);
    //子窗口
    this->placeWin = new PlaceMangerWindow();
    this->centraWid = new CentralWidget(this);
    this->centraWid->move(0,40);
    this->routeWid = new RoutePlanWid();
    this->resShowWid = new ResShowWid(this);
    this->resShowWid->move(1100,30);
    this->centraWid->show();
    //this->centraWid->hide();
    this->routeWid->hide();
    this->bicycleWin = new BicycleMangerWindow();
    this->bicycleWin->hide();
    this->aboutWid = new AboutWid();
    this->hide();
    this->MapPlaceSearch = new MapPlaceSearchWid(this);
    this->MapPlaceSearch->move(250,30);
    this->MapPlaceSearch->show();
    this->centraWid->move(0,60);
    this->helpWid = new HelpWid();
    this->helpWid->hide();
    this->placeWin->SetMainBox(this->MapPlaceSearch->getNameComBox());
    //事件
    connect(PlaceAction,&QAction::triggered,this,&MainWindow::ToPlaceWid);
    connect(PlanAction,&QAction::triggered,this,[=](){
        this->routeWid->show();
        this->routeWid->comBoxInit();
        resize(1500,800);
        this->centraWid->setFlagName((char *)"");
    });
    connect(BicycleAction,&QAction::triggered,this,[=]{
        this->bicycleWin->show();
        this->centraWid->setFlagName((char *)"");
    });
    connect(AboutAction,&QAction::triggered,this,[=](){
        this->aboutWid->show();
        this->centraWid->setFlagName((char *)"");
    });
    connect(DatainitAction,&QAction::triggered,[=](){
        placeWin->dataInit();
        this->centraWid->update();
        this->centraWid->setFlagName((char *)"");
    });
    connect(this->MapPlaceSearch->btn,&QPushButton::released,this,[=](){
        //QString name = this->MapPlaceSearch->nameEdit->text();
        QString name = this->MapPlaceSearch->nameBox->currentText();
        PlaceManger PM;
        if(PM.Search((char *)name.toStdString().c_str())==-1)
        {
            QMessageBox::information(this,"warning","您查找的地点不存在！");
            return ;
        }
        if(name=="")
        {
            QMessageBox::information(this,"warning","您输入的内容为空");
            return ;
        }
        this->centraWid->setFlagName((char *)name.toStdString().c_str());
    });
    connect(helpAction,&QAction::triggered,this,[=](){
        this->helpWid->show();
        this->centraWid->setFlagName((char *)"");
    });
    connect(BusAction,&QAction::triggered,this,[=](){
        QMessageBox::information(this,"......","该功能后期实现!");
    });
    //设置主窗口的内容
    //this->setCentralWidget(this->centraWid);
    connect(this->routeWid,&RoutePlanWid::RoutePlan,this->centraWid,&CentralWidget::Planing);
    connect(this->routeWid,&RoutePlanWid::RoutePlanGJ,this->centraWid,&CentralWidget::GJPlaning);
    connect(this->centraWid,&CentralWidget::sendLength,this,&MainWindow::AddItem);
    connect(this->centraWid,&CentralWidget::sendGJNode,this,&MainWindow::GJAddItem);
    connect(this->centraWid,&CentralWidget::sendAll,this,&MainWindow::AddAllItem);
    connect(this->routeWid,&RoutePlanWid::RoutePlanAll,this->centraWid,&CentralWidget::AllPlaning);
    this->row = 0;
    this->walkSpeed = 30;
    this->bicycleSpeed = 50;
    this->busSpeed = 200;
    this->eachWidth = 99;
}


void MainWindow::ToPlaceWid()
{
    this->centraWid->setFlagName((char *)"");
    this->placeWin->show();
}

void MainWindow::AddItem(int length)
{
    this->resShowWid->tableClear();
    row = 0;
    this->showBic(length);
    this->showWalk(length);
}

void MainWindow::GJAddItem(gjlx *path1, int length1,gjlx *path2, int length2)
{
    this->resShowWid->tableClear();
    row = 0;
    QTableWidgetItem *type = new QTableWidgetItem(QString("最短距离"));
    type->setBackground(QBrush(QColor("red")));
    this->resShowWid->Add(row++,type);
    this->showGJ(path1,length1);
    type = new QTableWidgetItem(QString("最少换乘"));
    type->setBackground(QBrush(QColor("red")));
    this->resShowWid->Add(row++,type);
    this->showGJ(path2,length2);
}

void MainWindow::AddAllItem(int length1, gjlx *path, int length,gjlx *path2,int length2)
{
    this->resShowWid->tableClear();
    row = 0;
    QTableWidgetItem *type = new QTableWidgetItem(QString("最少换乘"));
    type->setBackground(QBrush(QColor("red")));
    this->resShowWid->Add(row++,type);
    //换乘结果错误
    this->showMinStation(path2,length2,0);

    type = new QTableWidgetItem(QString("时间最少"));
    type->setBackground(QBrush(Qt::red));
    this->resShowWid->Add(row++,type);
    if(length1*eachWidth/bicycleSpeed<length*eachWidth/busSpeed+10)
    {
        this->showBic(length1,0);
    }
    else
    {
        this->showGJ(path,length,0);
    }
    type = new QTableWidgetItem(QString("路程最短"));
    type->setBackground(QBrush(Qt::red));
    this->resShowWid->Add(row++,type);
    this->showWalk(length1,0);
}



void MainWindow::showGJ(gjlx *path, int length,int flag)
{
    PlaceManger PM;

    QTableWidgetItem *item = new QTableWidgetItem("公交路线规划结果：");
    if(flag)
        item->setBackgroundColor(QColor("red"));
    this->resShowWid->Add(row++,item);
    int stationNum = 0;
    for(int i = 0;i<length;i++)
    {
        if(path[i].status==0&&i!=0&&i!=length-1)
            stationNum ++;
    }
    QString text1 = "全程" + QString::number(length*99) + "米，大约需要："
            +QString::number(length*eachWidth/busSpeed+(stationNum+1)*10,10,2) +"分钟！";
    this->resShowWid->Add(row++,new QTableWidgetItem(text1));
    QString text2 = "全程需换乘" + QString::number(stationNum) +"次";
    this->resShowWid->Add(row++,new QTableWidgetItem(text2));

    QString text3 = QString("从") + PM.getPlaceName(path[0].x,path[0].y) +"乘坐";
    if(path[0].status!=0)
        text3+=QString::number(path[0].status);
    else
        text3 += QString::number(path[1].status);
    text3 += "号线出发(绿色路线)";
    QTableWidgetItem *tips1 = new QTableWidgetItem(text3);
    tips1->setTextColor(QColor(Qt::green));
    this->resShowWid->Add(row++,tips1);
    int flag2 = 1;
    for(int i = 1;i<length-1;i++)
    {
        if(path[i].status==0)
        {
            QTableWidgetItem *temp = new QTableWidgetItem();
            QString text = QString("在") + PM.getPlaceName(path[i].x,path[i].y) + "转乘" + QString::number(path[i+1].status);
            text += "号线";
            if(flag2==1)
            {
                text += "(棕色路线)";
                temp->setTextColor(QColor(139, 69, 0));
            }
            else if(flag2 == 2)
            {
                text += "(红色路线)";
                temp->setTextColor(Qt::red);
            }
            else if(flag2 == 3)
            {
                text += "(蓝色路线)";
                temp->setTextColor(Qt::blue);
            }
            flag2 ++;
            temp->setText(text);
            this->resShowWid->Add(row++,temp);
        }
    }
    QString end;
    end = end +"在终点站" + PM.getPlaceName(path[length-1].x,path[length-1].y) + "下车";
    this->resShowWid->Add(row++,new QTableWidgetItem(end));
    QTableWidgetItem *info = new QTableWidgetItem(QString("图中公交的乘坐顺序为：绿、黄、红、蓝。"));
    this->resShowWid->Add(row++,info);
}

void MainWindow::showWalk(int length,int flag)
{
    QString text1 = "全程" + QString::number((length*eachWidth)) + "米，大约需要："
            +QString::number((length*eachWidth)/walkSpeed,10,2) +"分钟！";
    QTableWidgetItem *item = new QTableWidgetItem(text1);
    QTableWidgetItem *type = new QTableWidgetItem("步行线路规划（紫色线条表示）");
    if(flag)
        type->setBackgroundColor(QColor("red"));
    this->resShowWid->Add(row++,type);
    this->resShowWid->Add(row++,item);

}

void MainWindow::showMinStation(gjlx *path, int length, int)
{
    this->showGJ(path,length,0);
}

void MainWindow::showBic(int length,int flag)
{
    QTableWidgetItem *type = new QTableWidgetItem("骑行线路规划（紫色线条表示）");
    if(flag)
        type->setBackgroundColor(QColor("red"));
    this->resShowWid->Add(row++,type);
    QString text2 = "全程" + QString::number(length*99) + "米，大约需要："
            +QString::number(length*eachWidth/bicycleSpeed,10,2) +"分钟！";
    this->resShowWid->Add(row++,new QTableWidgetItem(text2));
}


MainWindow::~MainWindow()
{

}
